package com.JingYan.elasticSearch.service;

import com.JingYan.elasticSearch.domain.Book;
import com.JingYan.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * @author DongJiaQi
 * @create 2022/11/28 16:51
 * @Description:
 */
@Slf4j
@Service
public class BookService {
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private ESBookRepository esBookRepository;
    public void addBook(Book book) {
        try {
            esBookRepository.save(book);
        }catch (Exception e){
            log.error(String.format("保存ES错误！%s", e.getMessage()));
            throw new CustomException("保存ES错误");
        }
    }
    public List<Book> searchBook(String keyword){
        return esBookRepository.findByTitleOrAuthor(keyword, keyword);
    }
    public SearchHits<Book> searchBook1(String keyword){
        return esBookRepository.find(keyword);
    }
}
